﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class CheckOut : System.Web.UI.Page
{
    MobilDataContext ctx = new MobilDataContext();
    protected void Page_Load(object sender, EventArgs e)
    {
        // kiem tra dang nhap => lay username
        if (!IsPostBack)
        {
            if (Session["CurOrder"] != null)
            {
                ListOrdersItem curOrder = (ListOrdersItem)Session["CurOrder"];
                DataTable dtOrders = new DataTable("Orders");
                dtOrders.Columns.Add("No", Type.GetType("System.Int32"));
                dtOrders.Columns.Add("MobilId", Type.GetType("System.Int32"));
                dtOrders.Columns.Add("Quantity", Type.GetType("System.Int32"));
                dtOrders.Columns.Add("Price", Type.GetType("System.Int32"));
                dtOrders.Columns.Add("Total", Type.GetType("System.Int32"));

                int i = 1;
                int totalMoney = 0;
                foreach (var itm in curOrder.Cart)
                {
                    totalMoney += itm.Quantity * itm.Price;
                    dtOrders.Rows.Add(new object[] { i++, itm.MobilId, itm.Quantity, itm.Price, itm.Quantity * itm.Price });
                }

                GridView1.DataSource = dtOrders.DefaultView;
                GridView1.DataBind();
                lblTotal.Text = "Total Money: " + totalMoney;
            }
        }
    }
    protected void btnFinish_Click(object sender, EventArgs e)
    {
        
        if (Session["CurOrder"] != null)
        {
            ListOrdersItem curOrder = (ListOrdersItem)Session["CurOrder"];
            //int orderId =
            Order newOrder = new Order();
            newOrder.OId = MakeOrderId();
            newOrder.FullName = "QuangHD";  // lay username tu session
            newOrder.DateCreated = DateTime.Now;
            ctx.Orders.InsertOnSubmit(newOrder);
            ctx.SubmitChanges();

            foreach (OrdersItem itm in curOrder.Cart)
            {
                OrdersDetail od = new OrdersDetail();
                od.ODId = MakeOrderDetailId();
                od.OId = newOrder.OId;
                od.MobilId = itm.MobilId;
                od.Price = itm.Price;
                od.Quantity = itm.Quantity;
                ctx.OrdersDetails.InsertOnSubmit(od);
                ctx.SubmitChanges();
            }
            
            Session.Clear();    // xoa session => tao phien lam viec moi
            Response.Redirect("Default.aspx");
        }
    }
 
    private int MakeOrderId()
    {
        var nIds = from m in ctx.Orders
                   orderby m.OId descending
                   select m;
        int id = 0;
        if (nIds != null && nIds.Count() > 0)
        {
            Order m = nIds.ToArray()[0];
            id = m.OId;
        }
        return id + 1;
    }

    private int MakeOrderDetailId()
    {
        var nIds = from m in ctx.OrdersDetails
                   orderby m.ODId descending
                   select m;
        int id = 0;
        if (nIds != null && nIds.Count() > 0)
        {
            OrdersDetail m = nIds.ToArray()[0];
            id = m.ODId;
        }
        return id + 1;
    }
}